package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"zsc-boot/app/system_layer/model"
	"zsc-boot/app/system_layer/model/request"
	"zsc-boot/app/system_layer/service"
	"zsc-boot/core"
	"zsc-boot/core/web"
)

var (
	ServiceRoleApi     = new(serviceRoleApi)
	serviceRoleService = service.ServiceRoleService
)

type serviceRoleApi struct {
}

// CreateServiceRole 创建角色
func (s *serviceRoleApi) CreateServiceRole(ctx *gin.Context) {
	var serviceRole model.ServiceRole
	_ = ctx.BindJSON(&serviceRole)
	if err := serviceRoleService.CreateRole(serviceRole); err != nil {
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// DeleteServiceRole 删除角色
func (s *serviceRoleApi) DeleteServiceRole(ctx *gin.Context) {
	var serviceRole model.ServiceRole
	_ = ctx.BindJSON(&serviceRole)
	if err := serviceRoleService.DeleteServiceRole(serviceRole); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// UpdateServiceRole 更新角色
func (s *serviceRoleApi) UpdateServiceRole(ctx *gin.Context) {
	var serviceRole model.ServiceRole
	_ = ctx.BindJSON(&serviceRole)
	if err := serviceRoleService.UpdateServiceRole(serviceRole); err != nil {
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}

// GetServiceRole 查询角色详情
func (s *serviceRoleApi) GetServiceRole(ctx *gin.Context) {
	var id core.GetById
	_ = ctx.BindQuery(&id)
	if err, data := serviceRoleService.GetServiceRole(id.ID); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, data)
	}
}

// GetServiceRoleList 查询角色列表
func (s *serviceRoleApi) GetServiceRoleList(ctx *gin.Context) {
	var pageInfo request.ServiceRoleSearch
	_ = ctx.BindQuery(&pageInfo)
	if err, list, total := serviceRoleService.GetServiceRoleList(pageInfo); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessListResponse(ctx, list, total, pageInfo.Page, pageInfo.PageSize)
	}
}

// QueryMenuByRole 根据角色获取菜单权限
func (s *serviceRoleApi) QueryMenuByRole(ctx *gin.Context) {
	var queryMenuByRoleRequest request.QueryMenuByRoleRequest
	_ = ctx.BindQuery(&queryMenuByRoleRequest)
	if err, data := serviceRoleService.QueryMenuByRole(queryMenuByRoleRequest); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, data)
	}
}

// UpdateMenuByRole 根据角色更新菜单权限
func (s *serviceRoleApi) UpdateMenuByRole(ctx *gin.Context) {
	var updateMenuByRoleRequest request.UpdateMenuByRoleRequest
	_ = ctx.BindJSON(&updateMenuByRoleRequest)
	if err := serviceRoleService.UpdateMenuByRole(updateMenuByRoleRequest); err != nil {
		core.GetLogger().Error("操作失败", zap.Error(err))
		web.FailResponse(ctx, err, err.Error())
	} else {
		web.SuccessResponse(ctx, nil)
	}
}
